
//点击确认事件-取消图表隐藏（调整时间：202401091412）
const cancel = this.getApiPage().getApiWidgetByAliasName('cancel-event');
const cancelIcon = this.getApiPage().getApiWidgetByAliasName('取消图标');
const loading = this.pageObj.getApiWidgetByAliasName('loading')
const baocun = this.getApiPage().getApiWidgetByAliasName('detail-save')

const form = this.getApiPage().retrievePlugin('base-forms')
const data = form.getFormData('form_20')
const obj = Object.keys(data).reduce((acc, key) => {
  if (data[key] !== '') {
    acc[key] = data[key];
  }
  return acc;
}, {});

console.log(obj)

const levels = this.getApiPage().getDataById('744574925553008640')?.data || []
const types = this.getApiPage().getDataById('744575296061046784')?.data || []
const eventNatures = this.getApiPage().getDataById('756194853578080256')?.data || []
const project = this.getApiPage().getDataById('800024801300185088')?.data || [];
const sourceSystem = this.getApiPage().getDataById('800764940934709248')?.data || [];
const monitorList = this.getApiPage().getApiWidgetByAliasName('judge-content').widgetObj;
console.log('obj.....', monitorList)
obj.eventType = types.find(v => v.value === obj.eventTypeCode)?.name
obj.eventGrade = levels.find(v => v.value === obj.eventGradeCode)?.name
obj.eventQuality = eventNatures.find(v => v.value === obj.eventQualityCode)?.name
obj.whetherProcess = '2'
obj.projectName = project.find(v => v.value === obj.projectId)?.name;
obj.source = sourceSystem.find(v => v.value === obj.sourceSystemCode)?.name;
obj.sourceDevice = Array.isArray(monitorList?.tempData) && monitorList?.tempData.find(v => v.value === obj.sourceDeviceId)?.name;
const eventDetails = JSON.stringify({thumbnailPath: obj.thumbnailPath, jpbPath: obj.jpbPath})
delete obj.eventPoint
delete obj.thumbnailPath
delete obj.jpbPath
const params = {
  ...obj,
  eventDetails
}
console.log('save.....', params)
const txtList = {
  1 : '处置流程',
  2 : '应急联动',
  3 : '应急组织',
  4 : '应急人员',
  5 : '应急专家',
  6 : '应急物资',
  7 : '应急车辆',
}
const length = Object.keys(txtList).length;
let counter = 1, timer = null;
const options1 = {
  method: 'POST',
  headers: {
    "Content-type": "application/json",
  },
  body: JSON.stringify(params)
}

fetch('/Event/event/updateEvent', options1)
  .then(res => res.json())
  .then(json => {
    if (json?.resultCode === '000000') {
      loading.showWidget();
      cancelIcon.hideWidget()
      cancel.hideWidget()
      $(baocun.widgetObj.container).find('.label-text')[0].style.color = '#888'
      baocun.widgetObj.container.style.pointerEvents = 'none';

      const $form = this.getApiPage().getApiWidgetByAliasName('judge-content')
      $form.widgetObj.setData(obj)
      $form.widgetObj._option.isDetail = true
      $form.widgetObj.render()
      this.getApiPage().getApiWidgetByAliasName('三维组件').widgetObj.gisMap.showOrHideAllArea(false)
      this.getApiPage().setVariables([{ name: 'update', value: data.id }])
      const params = {
        ...$form.widgetObj._option.dataSource,
        ...obj
      }
      const that = this;
      timer = setInterval(() => {
        $('.loading-box .load-sell').text(txtList[counter]);
        counter++;
        if (counter > length) {
          $('.loading-box .load-text').html('完成...');
          $('.loading-box .load-sell-box').html('正在进入事件处置...');
          clearInterval(timer);

          window.parent.postMessage({ type: 'nav/style', pageName: '处置' }, document.referrer);
          that.getApiPage().navigateStage('759726721858535424', [{ name: 'screen_params', value: params }])
          loading.hideWidget()
        }
      }, 1000);

    }else{
      clearInterval(timer);
    }
  })
  .catch(err => console.error('分析研判失败', err))
  .finally(() => {
    // const judge = this.getApiPage().getApiWidgetsByClassName('judge')
    // judge.forEach(v => v.hideWidget())
    // const ico = this.getApiPage().getApiWidgetByAliasName('judge-ico')
    // ico.showWidget()
    // const save = this.getApiPage().getApiWidgetByAliasName('detail-judge')
    // save.showWidget()
    // $(baocun.widgetObj.container).find('.label-text')[0].style.color = '#FFF'
    // baocun.widgetObj.container.style.pointerEvents = 'auto'
  })
